/**
 * @Author: lusx
 * @Date:   11-03-2019 06:06:26
 * @Project: Govern督办
 * @Last modified by:   lusx
 * @Last modified time: 21-03-2019 04:45:50
 */
import React from "react";
import { observer, inject } from "mobx-react";
import { toJS } from "mobx";
import { ShareAuth, RouteLayout } from "weaver-mobile-page";
import { withRouter } from "react-router-dom";
import { Tools,Bubble,Toast} from 'weaver-mobile';
import MainListPage from '../../main/pages/mainListPage.js'
import * as Apis from "../../../apis/govern";
const getLabel = Tools.getLabel;

//@inject("actionSheetStore")
@inject("mainStore")
@withRouter
@observer
//共享组件
export default class ComShare extends React.Component {
  render() {
    const {
      mainStore :{actionSheetStore: { shareAuthRoute, shareCondition,list }}
    } = this.props;
    return (
      <div>
        <RouteLayout>
          <div
            key="wmPoutePageDemo"
            className="wm-route-layout wm-search-advanced-modal"
          >
            <ShareAuth
              customType={{
                skipEntry: true,
               // conditionData: toJS(shareCondition)
                conditionData:[shareCondition[0],shareCondition[1]]
              }}
              store={shareAuthRoute}
              label="共享权限"
              placeholder=""
              onChange={data => { this.onChange(data)}}
              >
                  <span style={{ color: '#55B1F9' }} className="gghh"> + {getLabel("000", '添加共享')}</span>
                  <div onClick={(e)=>{e.cancelBubble=true;e.stopPropagation()}}>
                     <MainListPage listViewStore= {list} extendBtn = {false} deleteshare = {this.deleteshare}/>
                  </div>
              </ShareAuth>
          </div>
        </RouteLayout>
      </div>
    );
  }
  onChange=(data)=>{
    const {
      mainStore :{actionSheetStore: {shareCondition,setShareSubData,handleSubmit,shareAuthRoute:{optionsForm},listGetDate,setshareAuthRoute }},history,match:{url}
    } = this.props;
    const result = optionsForm.doValidate();
    if (result.pass) {
      setShareSubData(data);
      handleSubmit().then((data)=>{
        Apis.getShareList({ id: data.id }).then(data => {
          listGetDate(data.sessionkey);
        });
        history.push(url);
        let index = url.lastIndexOf('/');
        const newurl = url.substr(0,index);
        setshareAuthRoute(newurl);
      });
    }else{
      return false;
    }

  }

  deleteshare =(relatedid) =>{
    const {
      mainStore :{actionSheetStore: {listGetDate}}} = this.props;
    let taskid = this.props.mainStore.actionSheetStore.id;

    Apis.delBatchShare({id:taskid,selectedId:relatedid}).then(data =>{
      Toast.hide();
      Toast.success(`${getLabel("83472", "删除成功")}`);
      listGetDate(data.sessionkey);
    })
  }
}
